package com.project.util;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AspectObj {

    @Before("execution(* com.project.service..*.*(..))")
    public void before() {
        System.out.println("方法执行前");
    }

    @After("execution(* com.project.service..*.*(..))")
    public void after() {
        System.out.println("后通知");
    }

    @Around("execution(* com.project.service..*.*(..))")
    public Object around(ProceedingJoinPoint point) {

        try {
            System.out.println("环绕前");
            //执行目标方法
            Object returnObj = point.proceed();
            System.out.println("环绕后");

            return returnObj;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    //抛出异常通知
    @AfterThrowing(value = "execution(* com.project.service..*.*(..))",
            throwing = "e")
    public void thowException(JoinPoint point,Exception e) {
        //得到目标对象
        Object target = point.getTarget();
        //得到目标方法名
        String methodName = point.getSignature().getName();
        System.out.println("执行"+target.getClass().getName()+"的"+methodName+"方法，抛出"+
            e.getClass().getName());
    }
}
